using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace FeatureWally.Client.UI.Controls {
    public class FWNodeFeature : TreeNode {
        private bool _isValid = true;
        private FWFeatureDefinition _definition;
        private string _featureXml = null;
        private string _stsAdm = null;

        public FWNodeFeature(FWFeatureDefinition definition)
            : base(definition.DisplayName, 7, 7) {
            _definition = definition;
            base.Tag = _definition.Definition;
        }

        public bool IsValid {
            get { return _isValid; }
            set { _isValid = value; }
        }

        public string GetFeatureXml() {
            if (_featureXml == null && _isValid) {
                try {
                    using (StreamReader sr = File.OpenText(_definition.Definition.RootDirectory + "\\feature.xml")) {
                        _featureXml = sr.ReadToEnd();
                    }
                }
                catch {
                    _featureXml = "N/A";
                }
            }
            return _featureXml;
        }

        public FWFeatureDefinition Definition {
            get { return _definition; }
        }

        private bool _includeHeader = true;
        private bool _activate = true;
        private bool _uninstall = true;
        
        public string GetStsAdm(bool includeHeader, bool activate, bool uninstall) {
            StringBuilder result = new StringBuilder();
            if (_isValid) {
                if (includeHeader) {
                    result.Append("REM Autogenerated STSADM commands to deactivate and uninstall features");
                    result.Append("\r\nREM Copy-Paste this to a .bat file to manually execute it.");
                    result.Append("\r\nREM Generated by Feature Wally");
                    result.AppendFormat("\r\n\r\n@SET STSADM=\"{0}\"", Properties.Settings.Default.STSADM);
                }

                result.AppendFormat("\r\n\r\nREM Feature: {0}", _definition.DisplayName);
                if (activate) {
                    if (_definition.ActivatedAt.Count > 0) {
                        if (_definition.Definition.Scope == SPFeatureScope.Farm) {
                            result.AppendFormat("\r\n%STSADM% -o deactivatefeature -name {0}", _definition.DisplayName);
                        }
                        else {
                            foreach (string url in _definition.ActivatedAt) {
                                result.AppendFormat("\r\n%STSADM% -o deactivatefeature -url \"{0}\" -name {1}", url, _definition.DisplayName);
                            }
                        }
                    }
                }
                if (uninstall) {
                    result.AppendFormat("\r\n%STSADM% -o uninstallfeature -name {0}", _definition.DisplayName);
                }
            }
            return result.ToString();
        }
    }
}
